#ifndef __BLOCK_POINTER_H__
#define __BLOCK_POINTER_H__

#include <cstring>
#include "file_pointer.h"

class BlockPointer : public FilePointer {
	public:
		static const unsigned char BLOCK_TYPE = 0;
	public: /* vale la pena ponerlos publicos dado que se los utiliza MUY seguidos */
		unsigned int reg;
		unsigned int block;
	public:
		BlockPointer(unsigned int reg,unsigned int block){
			this->reg = reg;
			this->block = block;
		}
		BlockPointer(const BlockPointer& reg){
			this->reg = reg.reg;
			this->block = reg.block;
		}
		~BlockPointer(){}

		unsigned char getType()const { return BLOCK_TYPE; }
		FilePointer* duplicate()const{ return new BlockPointer(*this); }
		unsigned int getBlock(){return block;}

		void operator =(const FilePointer& pointer){
			const BlockPointer& b2 = dynamic_cast<const BlockPointer&>(pointer);
			this->reg = b2.reg;
			this->block = b2.block;
		}

		unsigned int getSize()const{ return ((sizeof(unsigned int)*2)+FilePointer::getSize()); }

		std::ostream& toOstream(std::ostream& out)const{
			out << "\t\t-- BlockPointer --" << std::endl;
			out << "\t\t Numero de Registro  ="  << reg << std::endl;
			out << "\t\t Numero de Bloque    = " << block << std::endl;
			out << "\t\t-- Fin BlockPointer --" << std::endl;
			return out;
		}

		int compare(BlockPointer& bp){
			int ret=0;
			if(this->block<bp.block)
				ret=-1;
			else{
				if(this->block>bp.block)
					ret=1;
				else{
					if(this->reg<bp.reg) 
						ret=-1;
					else if(this->reg>bp.reg)
						ret=1;
				}
			}
			return ret;
		}

		char* toBytes(char* data)const{
			char* temp = data;
			temp = FilePointer::toBytes(temp);
			memcpy(temp,&reg,sizeof(unsigned int));
			temp += sizeof(unsigned int);
			memcpy(temp,&block,sizeof(unsigned int));
			temp += sizeof(unsigned int);
			return temp;
		}

		char* toFilePointer(char* data){
			char* temp = data;
			temp = FilePointer::toFilePointer(temp);
			memcpy(&reg,temp,sizeof(unsigned int));
			temp += sizeof(unsigned int);
			memcpy(&block,temp,sizeof(unsigned int));
			temp += sizeof(unsigned int);
			return temp;
		}
};

#endif /* __BLOCK_POINTER_H__ */
